
#include "area.h"
#include "avatar.h"
#include "commands-tedit.h"
#include "commandTable.h"
#include "commandTable-default.h"
#include "display.h"
#include "handler.h"
#include "room.h"
#include "symphony.h"
#include "world.h"

/*
TCmd::TCmd( void ) {
  name( "" );
  return;
}

bool TCmd::execute( Creature* creature, const std::string& args ) {

  return true;
}
*/

TCmdAddline::TCmdAddline( void ) {
  name( "addline" );
  shortcut( "+" );
  syntax( "syntax: addline <text>" );
  return;
}

bool TCmdAddline::execute( Creature* creature, const std::string& args ) {
  ((std::vector<std::string>*)creature->handler()->getState()["vector"])->push_back( args );
  creature->send( "Added \"{x%s\"{x\n", args.c_str() );
  return true;
}

TCmdDelete::TCmdDelete( void ) {
  name( "delete" );
  shortcut( "-" );
  syntax( "syntax: delete <line#>" );
  return;
}

bool TCmdDelete::execute( Creature* creature, const std::string& args ) {
  std::vector<std::string>* l = (std::vector<std::string>*)creature->handler()->getState()["vector"];
  std::vector<std::string> foo = *l;
  unsigned int line = estring(args);
  unsigned int i = 0;

  if ( line < 1 || line > 99 ) {
    creature->send( "Invalid line number." );
    return false;
  }

  l->clear();

  for ( std::vector<std::string>::iterator it = foo.begin(); it != foo.end(); ++it, ++i ) {
    if ( i != line-1 ) {
      l->push_back( *it );
    }
  }

  creature->send( "Deleted line %u\n", line );

  return true;
}

TCmdClear::TCmdClear( void ) {
  name( "clear" );
  return;
}

bool TCmdClear::execute( Creature* creature, const std::string& args ) {
  ((std::vector<std::string>*)creature->handler()->getState()["vector"])->clear();
  creature->send( "Text cleared." );
  return true;
}

TCmdInsert::TCmdInsert( void ) {
  name( "insert" );
  syntax( "syntax: insert <line#> <text>" );
  return;
}

bool TCmdInsert::execute( Creature* creature, const std::string& args ) {
  std::vector<std::string>* l = (std::vector<std::string>*)creature->handler()->getState()["vector"];
  std::vector<std::string> pieces = Regex::split( args, 1 );
  unsigned int line = 0;
  unsigned int i = 0;

  if ( pieces.size() != 2 ) {
    creature->send( syntax() );
    return false;
  }

  line = estring(pieces[0]);

  if ( line < 1 || line > 99 ) {
    creature->send( "Invalid line number." );
    return false;
  }

  for ( std::vector<std::string>::iterator it = l->begin(); it != l->end(); ++it, ++i ) {
    if ( i == line-1 ) {
      l->insert( it, pieces[1] );
      creature->send( "Inserted text at line %u\n", line );
      return true;
    }
  }

  l->push_back( pieces[1] );
  creature->send( "Inserted text at line %u\n", line );

  return true;
}

TCmdReplace::TCmdReplace( void ) {
  name( "replace" );
  syntax( "syntax: replace <line#> <text>" );
  return;
}

bool TCmdReplace::execute( Creature* creature, const std::string& args ) {
  std::vector<std::string> pieces = Regex::split( args, 1 );
  TCmdDelete del;
  TCmdInsert ins;

  del.execute( creature, pieces[0] );
  ins.execute( creature, args );

  return true;
}

TCmdShow::TCmdShow( void ) {
  name( "show" );
  return;
}

bool TCmdShow::execute( Creature* creature, const std::string& args ) {
  std::vector<std::string>* l = (std::vector<std::string>*)creature->handler()->getState()["vector"];
  char buffer[MAX_BUFFER];
  std::string output;
  unsigned int u = 0;

  if ( l->empty() ) {
    creature->send( "There is no text in the buffer at the moment." );
    return true;
  }

  output.append( "+--+" ).append( 82, '-' ).append( "+\n" );
  for ( std::vector<std::string>::iterator it = l->begin(); it != l->end(); ++it ) {
    sprintf( buffer, "|{y%2u{x| %-80s |\n", ++u, it->c_str() );
    output.append( buffer );
  }
  output.append( "+--+" ).append( 82, '-' ).append( "+\n" );

  creature->send( output );
  return true;
}

